package com.cucn.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cucn.commonutils.Result;
import com.cucn.educms.entity.CrmBanner;
import com.cucn.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 后台前端控制器
 * </p>
 *
 * @author KouChaoJie
 * @since 2021-10-09
 */
@RestController
//@CrossOrigin
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService crmBannerService;

    /**
     * 分页查询banner
     *
     * @param limit 每页显示记录数
     * @param page  当前页
     * @return Result统一结果返回每页数据集合, 总记录数
     */
    @GetMapping("/pageBanner/{page}/{limit}")
    public Result pageBanner(@PathVariable long limit, @PathVariable long page) {
        Page<CrmBanner> pageBanner = new Page<>(page, limit);
        crmBannerService.page(pageBanner, null);

        return Result.ok().data("items", pageBanner.getRecords()).data("total", pageBanner.getTotal());
    }

    /**
     * 添加banner
     *
     * @param crmBanner 需要添加的banner请求体对象
     */
    @PostMapping("/addBanner")
    public Result addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return Result.ok();
    }

    /**
     * 根据id查询banner
     *
     * @param id bannerId
     * @return Result统一结果返回
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return Result.ok().data("items", banner);
    }

    /**
     * 修改Banner
     *
     * @param crmBanner 修改的banner
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody CrmBanner crmBanner) {
        crmBannerService.updateById(crmBanner);
        return Result.ok();
    }

    /**
     * 删除banner
     *
     * @param id bannerId
     * @return Result统一结果返回
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return Result.ok();
    }
}

